﻿#include "orderformlist.h"
#include "ui_orderformlist.h"
#include <QDebug>
#include <QLabel>
#include <QMessageBox>

OrderFormList::OrderFormList(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::OrderFormList)
{
    ui->setupUi(this);

    m_comment = nullptr;
    m_reserveDetails = nullptr;
    for (int i = 0; i < 5; i++)
    {
        m_contentCount[i] = 0;
    }
    ui->tabWidget->setCurrentIndex(0);
}

OrderFormList::~OrderFormList()
{
    delete ui;
}

void OrderFormList::reserveListClean()
{
    if (m_comment != nullptr)
    {
        delete m_comment;
        m_comment = nullptr;
    }
    if (m_reserveDetails != nullptr)
    {
        delete m_reserveDetails;
        m_reserveDetails = nullptr;
    }

    while (ui->lwg_all->count() > 0)
    {
        QListWidgetItem * item = ui->lwg_all->takeItem(0);
        delete item;
    }
    while (ui->lwg_noComment->count() > 0)
    {
        QListWidgetItem * item = ui->lwg_noComment->takeItem(0);
        delete item;
    }
    while (ui->lwg_noPay->count() > 0)
    {
        QListWidgetItem * item = ui->lwg_noPay->takeItem(0);
        delete item;
    }
    while (ui->lwg_noUse->count() > 0)
    {
        QListWidgetItem * item = ui->lwg_noUse->takeItem(0);
        delete item;
    }
    while (ui->lwg_refund->count() > 0)
    {
        QListWidgetItem * item = ui->lwg_refund->takeItem(0);
        delete item;
    }

    for (int i = 0; i < 5; i++)
    {
        m_contentCount[i] = 0;
    }

    m_orderFormContentVector.clear();
    m_orderContentVector.clear();
    ui->tabWidget->show();
}

void OrderFormList::slotSetReserveList(bool result,const QString& state,const QString& commentState)
{
    if (result)
    {
        OrderFormContent *orderFormContent = new OrderFormContent();
        m_orderFormContentVector.append(orderFormContent);
        if (ui->lwg_all->count() == m_contentCount[0])
        {
            ui->lwg_all->addItem("");
        }
        ui->lwg_all->setItemWidget(ui->lwg_all->item(m_contentCount[0]),orderFormContent);
        ui->lwg_all->item(m_contentCount[0])->setSizeHint(QSize(0, 140));  //每次改变Item的高度

        m_contentCount[0]++;
//        connect(orderFormContent,SIGNAL(signalReserveDetails(const ReserveInfo&)),
//                this, SLOT(slotReserveDetails(const ReserveInfo&)));
//        connect(order,SIGNAL(signalGainHotelDetails(QString)),
//                this, SLOT(slotGainHotelDetails(QString)));
        connect(orderFormContent,SIGNAL(signalCommentReserve(const QStringList&)),
                this, SLOT(slotCommentReserve(const QStringList&)));
        connect(orderFormContent,SIGNAL(signalReserveDetails(const QString&)),
                this, SIGNAL(signalGainReserveDetails(const QString&)));
        qDebug() << commentState;
        if(commentState == "已评论")
        {
            orderFormContent->setHaveComented();
            return;
        }

        OrderFormContent *order = new OrderFormContent();
        m_orderContentVector.append(order);
        connect(order,SIGNAL(signalCommentReserve(const QStringList&)),
                this, SLOT(slotCommentReserve(const QStringList&)));
        connect(order,SIGNAL(signalReserveDetails(const QString&)),
                this, SIGNAL(signalGainReserveDetails(const QString&)));
        if (state == "待付款")
        {
            if (ui->lwg_noPay->count() == m_contentCount[1])
            {
                ui->lwg_noPay->addItem("");
            }
            ui->lwg_noPay->setItemWidget(ui->lwg_noPay->item(m_contentCount[1]),order);
            ui->lwg_noPay->item(m_contentCount[1])->setSizeHint(QSize(0, 140));
            m_contentCount[1]++;
        }else if (state == "待使用")
        {
            if (ui->lwg_noUse->count() == m_contentCount[2])
            {
                ui->lwg_noUse->addItem("");
            }
            ui->lwg_noUse->setItemWidget(ui->lwg_noUse->item(m_contentCount[2]),order);
            ui->lwg_noUse->item(m_contentCount[2])->setSizeHint(QSize(0, 140));
            m_contentCount[2]++;
        }else if (state == "待评价")
        {
            if (ui->lwg_noComment->count() == m_contentCount[3])
            {
                ui->lwg_noComment->addItem("");
            }
            ui->lwg_noComment->addItem("");
            ui->lwg_noComment->setItemWidget(ui->lwg_noComment->item(m_contentCount[3]),order);
            ui->lwg_noComment->item(m_contentCount[3])->setSizeHint(QSize(0, 140));
            m_contentCount[3]++;
        }else if (state == "退款")
        {
            if (ui->lwg_refund->count() == m_contentCount[4])
            {
                ui->lwg_refund->addItem("");
            }
            ui->lwg_refund->addItem("");
            ui->lwg_refund->setItemWidget(ui->lwg_refund->item(m_contentCount[4]),order);
            ui->lwg_refund->item(m_contentCount[4])->setSizeHint(QSize(0, 140));
            m_contentCount[4]++;
        }
        else{
            ;
        }
    }
    else
    {
        qDebug() << state;
        if (state == "您还没有订单哦")
        {
            ui->lwg_all->close();
            ui->lwg_noUse->close();
            ui->lwg_noPay->close();
            ui->lwg_noComment->close();
            ui->lwg_refund->close();
        }else
        {
            if (ui->lwg_all->count() == 0)
            {
                ui->lwg_all->close();
            }else
            {
                ui->lwg_all->show();
            }

            if (ui->lwg_noUse->count() == 0)
            {
                ui->lwg_noUse->close();
            }else
            {
                ui->lwg_noUse->show();
            }

            if (ui->lwg_noPay->count() == 0)
            {
                ui->lwg_noPay->close();
            }else
            {
                ui->lwg_noPay->show();
            }

            if (ui->lwg_noComment->count() == 0)
            {
                ui->lwg_noComment->close();
            }else
            {
                ui->lwg_noComment->show();
            }

            if (ui->lwg_refund->count() == 0)
            {
                ui->lwg_refund->close();
            }else
            {
                ui->lwg_refund->show();
            }

        }
    }
}

void OrderFormList::slotCommentReserve(const QStringList& strList)
{
    m_comment = new OrderFormComment();
    m_comment->setWindowTitle("评价酒店");
    m_comment->setWindowModality(Qt::ApplicationModal);
    m_comment->setWindowFlags(windowFlags()&~Qt::WindowMaximizeButtonHint); // 禁止最大化按钮
    m_comment->setFixedSize(m_comment->width(),m_comment->height());               // 禁止拖动窗口大小
    connect(m_comment,SIGNAL(signalCommentSubmit()),
            this,SIGNAL(signalReserveCommentSubmit()));
    m_comment->setHotel(strList);
    m_comment->show();
}

void OrderFormList::slotSubmitCommentResult(bool result,QString msg)
{
    if (result) /// 接收图片成功即为 评论成功
    {
        reserveListClean();
        emit signalGainReserveList();
    }
    else
    {
        m_comment->setResult(msg);
    }
}

void OrderFormList::slotSetReserveHotelImage(bool result,const QString& reserveID,const QImage& image)
{
    if (result)
    {
        for (int j = 0; j < m_contentCount[0]; j++)             /// 全部
        {
            if (reserveID == m_orderFormContentVector[j]->getReserveId())
            {
                 m_orderFormContentVector[j]->setImage(image);
            }
        }
        for (int i = 0; i < m_orderContentVector.size(); i ++)            /// 其他
        {
            if (reserveID == m_orderContentVector[i]->getReserveId())
            {
                 m_orderContentVector[i]->setImage(image);
            }
        }
    }
}

void OrderFormList::slotSetReserveDetails(bool result,const QString& msg)
{
    if (result)
    {
        m_reserveDetails = new OrderFormDetails();
        m_reserveDetails->setWindowTitle("订单详情");
        m_reserveDetails->setWindowModality(Qt::ApplicationModal);
        m_reserveDetails->setWindowFlags(windowFlags()&~Qt::WindowMaximizeButtonHint); // 禁止最大化按钮
        m_reserveDetails->setFixedSize(m_reserveDetails->width(),m_reserveDetails->height()); // 禁止拖动窗口大小
        m_reserveDetails->initWindowsUnit(GlobalVars::g_localReserve);
        m_reserveDetails->show();
        connect(m_reserveDetails,SIGNAL(signalDeleteReserveRecord(const QString&)),
                this,SIGNAL(signalDeleteReserve(const QString&)));
        connect(m_reserveDetails,SIGNAL(signalHotelDetails(const QString&)),
                this,SIGNAL(signalGainHotelDetails(const QString&)));
    }
    else
    {
        QMessageBox::warning(this, "错误", msg, QMessageBox::Yes, QMessageBox::Yes);
    }
}

void OrderFormList::slotReserveDeleteResult(bool result,const QString& msg)
{
    m_reserveDetails->reserveDeleteResult(result,msg);
    if (result)
    {
        reserveListClean();
    }
}

